<?php

namespace addons\booking\model;

use think\Model;


class Store extends Model
{

    // 表名
    protected $name = 'booking_store';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];

    public function getImagesAttr($value, $data)
    {
        $value = $value ? $value : ($data['images'] ?? '');
        if (!$value) {
            return [];
        }
        $value = array_filter(explode(',', $value));
        foreach ($value as &$item) {
            $item = cdnurl($item, true);
        }
        return $value;
    }

    public function getImageAttr($value, $data)
    {
        $value = $value ? $value : ($data['images'] ?? '');
        if (!$value) {
            return '';
        }
        $value = array_filter(explode(',', $value));

        return isset($value[0]) ? cdnurl($value[0], true) : '';
    }

    /**
     * 门店列表
     *
     * @param array $param
     * @return \think\Paginator
     */
    public static function tableList($param)
    {
        $pageNum = 15;
        if (isset($param['num']) && !empty($param['num'])) {
            $pageNum = $param['num'];
        }
        $field = ['id', 'name', 'intro', 'images', 'address', 'phone', 'latitude', 'longitude', 'createtime'];
        //根据距离取值
        $PI = M_PI;
        if (isset($param['latitude']) && isset($param['longitude']) && !empty($param['latitude']) && !empty($param['longitude'])) {
            $field[] = "(2 * 6378.137 * ASIN(SQRT(POW(SIN($PI*({$param['latitude']}-latitude)/360),2)+COS($PI*{$param['latitude']}/180)* COS(latitude * $PI/180)*POW(SIN($PI*({$param['longitude']}-longitude)/360),2)))) as distance";
        }
        $list = self::field($field)
            ->where(function ($query) use ($param) {
            })
            ->where('status', 'normal')
            ->order('weigh desc,id desc')
            ->paginate($pageNum);

        foreach ($list as $item) {
            $item->append(['image']);
            $item->hidden(['images']);
        }

        return $list;
    }

    /**
     * 已入驻酒店列表
     *
     * @param array $param
     * @return \think\Paginator
     */
    public static function getSettleStore($param)
    {
        $pageNum = 20;
        if (isset($param['page']) && !empty($param['num'])) {
            $pageNum = $param['num'];
        }
        $field = ['id', 'name', 'images', 'address', 'phone', 'latitude', 'longitude', 'createtime'];
        //根据距离取值
        $PI = M_PI;
        if (isset($param['latitude']) && isset($param['longitude']) && !empty($param['latitude']) && !empty($param['longitude'])) {
            $field[] = "(2 * 6378.137 * ASIN(SQRT(POW(SIN($PI*({$param['latitude']}-latitude)/360),2)+COS($PI*{$param['latitude']}/180)* COS(latitude * $PI/180)*POW(SIN($PI*({$param['longitude']}-longitude)/360),2)))) as distance";
        }
        $where['apply_status'] = ['=',1];
        $list = self::field($field)
            ->where(function ($query) use ($param) {
            })
            ->where('status', 'normal')
            ->order('weigh desc,id desc')
            ->paginate($pageNum);

        foreach ($list as $item) {
            $item->append(['image']);
            $item->hidden(['images']);
            $item['order_num'] = 0;
        }

        return $list;
    }

    public static function getDetail($id)
    {
        return self::where('id', $id)->find();
    }


    public static function addStore($request = [])
    {

        $res = self::create($request);
        return $res;
    }
}
